package cn.opopopo.design_pattern_learn.行为型.visitor;

// 1. 导出为 HTML 的访问者
class HtmlExportVisitor implements DocumentElementVisitor {
    private StringBuilder html = new StringBuilder();

    @Override
    public void visit(TextElement text) {
        html.append("<p>").append(text.getContent()).append("</p>\n");
    }

    @Override
    public void visit(ImageElement image) {
        html.append("<img src=\"").append(image.getImageUrl())
            .append("\" size=\"").append(image.getSize()).append("KB\" />\n");
    }

    @Override
    public void visit(TableElement table) {
        html.append("<table border='1'>\n");
        for (int i = 0; i < table.getRows(); i++) {
            html.append("  <tr>");
            for (int j = 0; j < table.getCols(); j++) {
                html.append("<td>Cell</td>");
            }
            html.append("</tr>\n");
        }
        html.append("</table>\n");
    }

    public String getHtml() {
        return html.toString();
    }
}